package com.zycfc.zsf.boot.mq.rabbitmq.util;

import javax.sql.*;
import java.sql.*;
import java.util.*;
import org.slf4j.*;

public class DbUtils
{
    private static final Logger log;
    
    public static List<Map<String, Object>> executeList(final DataSource dataSource, final String sql, final Object[] args) {
        return execute(dataSource, (JdbcCallback<List<Map<String, Object>>>)new JdbcCallback<List<Map<String, Object>>>() {
            @Override
            public List<Map<String, Object>> doInConnection(final Connection c) {
                PreparedStatement ps = null;
                ResultSet rs = null;
                try {
                    ps = c.prepareStatement(sql);
                    if (args != null && args.length > 0) {
                        for (int i = 0; i < args.length; ++i) {
                            DbUtils.log.debug(String.valueOf(args[i]));
                            ps.setObject(i + 1, args[i]);
                        }
                    }
                    rs = ps.executeQuery();
                    final ResultSetMetaData md = rs.getMetaData();
                    final int columnSize = md.getColumnCount();
                    final List<String> fieldNameList = new ArrayList<String>();
                    for (int j = 1; j <= columnSize; ++j) {
                        DbUtils.log.info(md.getColumnName(j) + "," + md.getColumnLabel(j));
                        fieldNameList.add(md.getColumnLabel(j));
                    }
                    final List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();
                    Map<String, Object> data = null;
                    while (rs.next()) {
                        data = new HashMap<String, Object>();
                        for (final String fieldName : fieldNameList) {
                            final Object val = rs.getObject(fieldName);
                            data.put(fieldName, val);
                        }
                        datas.add(data);
                    }
                    return datas;
                }
                catch (SQLException e) {
                    DbUtils.log.error("SQLException211", (Throwable)e);
                    return null;
                }
                finally {
                    try {
                        if (ps != null) {
                            ps.close();
                        }
                        if (rs != null) {
                            rs.close();
                        }
                    }
                    catch (SQLException e2) {
                        DbUtils.log.error("SQLException218", (Throwable)e2);
                    }
                }
            }
        });
    }
    
    public static String executeQuery(final DataSource dataSource, final String sql) {
        return execute(dataSource, (JdbcCallback<String>)new JdbcCallback<String>() {
            @Override
            public String doInConnection(final Connection c) {
                PreparedStatement ps = null;
                ResultSet rs = null;
                try {
                    ps = c.prepareStatement(sql);
                    rs = ps.executeQuery();
                    rs.next();
                    return rs.getString(1);
                }
                catch (SQLException e) {
                    DbUtils.log.error("SQLException210", (Throwable)e);
                    return null;
                }
                finally {
                    try {
                        if (ps != null) {
                            ps.close();
                        }
                        if (rs != null) {
                            rs.close();
                        }
                    }
                    catch (SQLException e2) {
                        DbUtils.log.error("SQLException218", (Throwable)e2);
                    }
                }
            }
        });
    }
    
    public static <T> T executeQueryObject(final DataSource dataSource, final String sql, final Object[] args) {
        return execute(dataSource, (JdbcCallback<T>)new JdbcCallback<T>() {
            @Override
            public T doInConnection(final Connection c) {
                PreparedStatement ps = null;
                ResultSet rs = null;
                try {
                    ps = c.prepareStatement(sql);
                    if (args != null) {
                        for (int i = 0; i < args.length; ++i) {
                            ps.setObject(i + 1, args[i]);
                        }
                    }
                    rs = ps.executeQuery();
                    rs.next();
                    return (T)rs.getObject(1);
                }
                catch (SQLException e) {
                    DbUtils.log.error("SQLException210", (Throwable)e);
                    return null;
                }
                finally {
                    try {
                        if (ps != null) {
                            ps.close();
                        }
                        if (rs != null) {
                            rs.close();
                        }
                    }
                    catch (SQLException e2) {
                        DbUtils.log.error("SQLException218", (Throwable)e2);
                    }
                }
            }
        });
    }
    
    public static boolean executeUpdate(final DataSource dataSource, final String sql, final Object[] args) {
        return execute(dataSource, (JdbcCallback<Boolean>)new JdbcCallback<Boolean>() {
            @Override
            public Boolean doInConnection(final Connection c) {
                PreparedStatement ps = null;
                try {
                    ps = c.prepareStatement(sql);
                    if (args != null) {
                        for (int i = 0; i < args.length; ++i) {
                            ps.setObject(i + 1, args[i]);
                        }
                    }
                    return ps.execute();
                }
                catch (SQLException e) {
                    DbUtils.log.error("SQLException81", (Throwable)e);
                    return false;
                }
                finally {
                    try {
                        if (ps != null) {
                            ps.close();
                        }
                    }
                    catch (SQLException e2) {
                        DbUtils.log.error("SQLException189", (Throwable)e2);
                    }
                }
            }
        });
    }
    
    private static <T> T execute(final DataSource dataSource, final JdbcCallback<T> callback) {
        Connection c = null;
        try {
            c = dataSource.getConnection();
            return callback.doInConnection(c);
        }
        catch (SQLException e2) {
            DbUtils.log.error("\u6267\u884c\u5f02\u5e38");
            return null;
        }
        finally {
            if (c != null) {
                try {
                    c.close();
                }
                catch (SQLException e) {
                    DbUtils.log.error("execute--SQLException", (Throwable)e);
                }
            }
        }
    }
    
    static {
        log = LoggerFactory.getLogger((Class)DbUtils.class);
    }
    
    private interface JdbcCallback<T>
    {
        T doInConnection(final Connection p0);
    }
}
